package com.xiaouyudeguang.common.around;

import com.xiaouyudeguang.common.annotation.ApiLog;
import com.xiaouyudeguang.common.aspect.LogAspect;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Aspect
@Component
@AllArgsConstructor
public class LogAround extends LogAspect {

    @SneakyThrows
    @Around("@annotation(apiLog)")
    public Object around(ProceedingJoinPoint pjp, ApiLog apiLog) {
        super.initAspect(pjp);
        Service service = realClass.getAnnotation(Service.class);
        // 判断类级别关闭
        if (service == null) {
            return pjp.proceed();
        }
        if (apiLog == null) {
            apiLog = AnnotationUtils.findAnnotation(realMethod, ApiLog.class);
        }
        if (apiLog == null || apiLog.close()) {
            return pjp.proceed();
        }
        return doPrint(pjp, realClass.getName() + "." + realMethod.getName(), apiLog.tags());
    }
}
